package com.third.githublogin.config;

import com.third.githublogin.oauth.api.GithubApi;
import com.third.githublogin.oauth.service.GithubOAuthService;
import com.third.githublogin.oauth.service.OAuthServiceDeractor;
import org.scribe.builder.ServiceBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration

public class OAuthConfig {
    private static final String CALLBACK_URL = "%s/oauth/%s/callback";

    @Value("${oAuth.github.state}")
    String state;

    @Value("${oAuth.github.clientId}")
    String githubClientId;

    @Value("${oAuth.github.clientSercret}")
    String githubclientSecret;

    @Value("${demo.host}")
    String host;

    @Bean
    public GithubApi githubApi(){
        return new GithubApi(state);
    }

    @Bean
    public OAuthServiceDeractor getGithubOAuthService(){
        return new GithubOAuthService(new ServiceBuilder()
                .provider(githubApi())
                .apiKey(githubClientId)
                .apiSecret(githubclientSecret)
                .callback(String.format(CALLBACK_URL,host,OAuthTypes.GITHUB))
                .build());
    }
}
